package com.dou.sample.opengl_project.data;

import android.opengl.GLES20;

import com.dou.sample.opengl_project.shader.TextureShaderProgram;
import com.dou.sample.opengl_project.utls.Constants;

/**
 * Created by Dou on 2018/1/16.
 */

public class Table {
    private static final int POSITION_COMPONENT_COUNT = 2;
    private static final int TEXTURE_COODINATES_COMPONENT_COUNT = 2;
    private static final int STRIDE = (POSITION_COMPONENT_COUNT + TEXTURE_COODINATES_COMPONENT_COUNT) * Constants.BYTES_PER_FLOAT;

    private VertexArray vertexArray;

    private static final float[] VERTEX_DATA = {
            // order of coodinates
            // X, Y, S, T

            0,0,.5f,.5f,
            -0.5f, -0.8f,0, .9f,
            0.5f, -0.8f,1,.9f,
            0.5f, 0.8f,1,.1f,
            -0.5f,0.8f,0,.1f,
            -0.5f, -0.8f,0,.9f
    };

    public Table(){
        vertexArray = new VertexArray(VERTEX_DATA);
    }

    public void bindData(TextureShaderProgram textureShaderProgram){
        // 把顶点数组绑定到着色器程序上
        vertexArray.setVertexAttributePointer(
                0,
                textureShaderProgram.getaPositionLocation(),
                POSITION_COMPONENT_COUNT,
                STRIDE
        );

        vertexArray.setVertexAttributePointer(
                POSITION_COMPONENT_COUNT,
                textureShaderProgram.getaTextureCoodinatesLocation(),
                TEXTURE_COODINATES_COMPONENT_COUNT,
                STRIDE
        );
    }

    public void draw(){
        // 绘制
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 6);
    }
}
